{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# scikit-learn → PMML\n",
    "\n",
    "\n",
    "### Exporter: Gradient Boosting\n",
    "### Data Set used: Titanic\n",
    "\n",
    "\n",
    "### **STEPS**: \n",
    "- Build the Pipeline with preprocessing (using DataFrameMapper)\n",
    "- Build PMML using Nyoka exporter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing, Model building (using pipeline) for Titanic data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:27:16.921866Z",
     "start_time": "2018-08-13T17:27:15.846866Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler, Imputer, LabelEncoder, LabelBinarizer\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "\n",
    "titanic = pd.read_csv(\"titanic_train.csv\")\n",
    "\n",
    "titanic['Embarked'] = titanic['Embarked'].fillna('S')\n",
    "\n",
    "features = list(titanic.columns.drop(['PassengerId','Name','Ticket','Cabin','Survived']))\n",
    "target = 'Survived'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:27:19.458366Z",
     "start_time": "2018-08-13T17:27:19.403366Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\vran\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py:95: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n",
      "C:\\Users\\vran\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\label.py:128: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('mapping', DataFrameMapper(default=False, df_out=False,\n",
       "        features=[(['Sex'], LabelEncoder()), (['Embarked'], LabelEncoder())],\n",
       "        input_df=False, sparse=False)), ('imp', Imputer(axis=0, copy=True, missing_values='NaN', strategy='median', verbose=0)), ('gbc', GradientBoostingClass...      presort='auto', random_state=None, subsample=1.0, verbose=0,\n",
       "              warm_start=False))])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline_obj = Pipeline([\n",
    "    (\"mapping\", DataFrameMapper([\n",
    "        (['Sex'], LabelEncoder()),\n",
    "        (['Embarked'], LabelEncoder())\n",
    "    ])),\n",
    "    (\"imp\", Imputer(strategy=\"median\")),\n",
    "    (\"gbc\", GradientBoostingClassifier(n_estimators = 10))\n",
    "])\n",
    "\n",
    "pipeline_obj.fit(titanic[features],titanic[target])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export the Pipeline object into PMML using the Nyoka package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:27:32.518866Z",
     "start_time": "2018-08-13T17:27:31.881866Z"
    }
   },
   "outputs": [],
   "source": [
    "from nyoka import skl_to_pmml\n",
    "\n",
    "skl_to_pmml(pipeline_obj, features, target, \"gb_pmml.pmml\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
